<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Auth;
class SessionsController extends Controller
{
    public function __construct(){
        $this->middleware('guest',[
            'only' => ['create']
        ]
        );
    }
    public function create()
    {
        return view('sessions.create');
    }
    public function store(Request $request)
    {
        $credentials = $this->validate($request, [
            'email' => 'required|email|max:255',
            'password' => 'required'
            ]);
        if(Auth::attempt($credentials)){
            //login success
            session()->flash('success', 'welcome back.');
            return redirect()->route('users.show', [Auth::user()]);
        }else{
            session()->flash('danger', 'password and email mismatched.');
            //login failure.
            return redirect()->back()->withInput();
        }
    }
    public function destroy()
    {
        Auth::logout();
        session()->flash('success', '您已经退出');
        return redirect('login');
    }

}
